package alg20241018;

public class Text {
    public void duplicateZeros(int[] arr) {
        int cur = 0;
        int dest = -1;
        int n = arr.length;
        //1.先找到cur指向的最后一个数
        while(cur < n){
            if(arr[cur] != 0) {
                dest++;
            }else{
                dest += 2;
            }
            if(dest >= n-1){
                break;
            }
            cur++;
        }
        //2.处理边界问题
        if(dest == n){
            arr[n-1] = 0;
            cur--;
            dest -= 2;
        }
        //3.从后往前复写
        while(cur >= 0){
            if(arr[cur] != 0){
                arr[dest--] = arr[cur--];
            }else{
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            }
        }

    }

}
